<?php

namespace App\Http\Controllers\Home\Set;

use App\Http\Controllers\Controller;
use App\Models\Home\Users;
use App\User;
use Illuminate\Http\Request;
use App\Models\Home\Frame as FrameModel;
use App\Models\Home\Users as UsersModel;
use Illuminate\Support\Facades\Hash;
use Frame;
use Branch;
use Tools;
use Auth;

class FrameController extends Controller
{
    protected $roleList = ['admin','leader','staff','external','recommend'];
    protected $passField = [
        'username','describe','password','frame_id','type','status','created_at','updated_at'
    ];

    public function frame(Request $request)
    {
        if($request->isMethod('post')){

            $frame = Frame::GetAll();

            $return = [
                'msg'=> "",
                'code'=> 0,
                'data' => $frame,
                'count'=> count($frame),
                'is'=> true,
                'tip'=> '操作成功！'
            ];

            return $return;

        }

        return view('Home.User.Set.Frame.frame');
    }

    public function frame_admin_add(){
        if(request()->isMethod('post')){
            //验证
            $this->validate(request(), [
                'username' => ['required','string',function($attribute, $value, $fail){
                    $user = UsersModel::where('username',$value)->where('status',1)->first();
                    if ($user){
                        $fail(':attribute 已存在。');
                    }
                }],
                'branch_id' => [function ($attribute, $value, $fail) {
                    if (!intval($value)) {
                        $fail(':attribute 未选择。');
                    }
                }],
                'describe' => 'nullable|string',
                'password' => 'required|string|min:8',
            ],[],[
                'username' => '用户名',
                'branch_id' => '所属部门',
                'describe' => '描述',
                'password' => '密码',
            ]);

            //数据库操作
            $user_data = [];
            $frame_data = [];
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && trim($val)){
                    if($key == 'password' && $val){
                        $val = Hash::make($val);
                    }
                    $user_data[$key] = $val;
                }
            }
            $user = Users::create($user_data);

            $frame_data['branch_id'] = intval(request()->post('branch_id'));
            $frame_data['level'] = 1;
            $frame_data['uid'] = $user->id;
            $frame_data['belong'] = 0;
            $frame = Frame::create($frame_data);

            $user->frame_id = $frame->id;

            $user->save();

            $user_info = User::with('frame')->find($user->id);
            if ($user_info && $user_info->type == 1){
                if ($user_info->frame){
                    switch ($user_info->frame->level ){
                        case 1:
                            $user_info->assignRole('admin');
                            break;
                        case 2:
                            $user_info->assignRole('leader');
                            break;
                        case 3:
                            $user_info->assignRole('staff');
                            break;
                    }
                }
            }elseif ($user_info && $user_info->type == 2){
                $user_info->assignRole('external');
            }elseif ($user_info && $user_info->type == 3){
                $user_info->assignRole('recommend');
            }

            //返回结果
            return ['code'=>1];
        }

        $branch = Branch::GetBranch();

        return view('Home.User.Set.Frame.admin_add',compact('obj','branch'));
    }

    public function frame_add(FrameModel $obj){
        if(request()->isMethod('post')){
            //验证
            $this->validate(request(), [
                'username' => ['required','string',function($attribute, $value, $fail){
                    $user = UsersModel::where('username',$value)->where('status',1)->first();
                    if ($user){
                        $fail(':attribute 已存在。');
                    }
                }],
                'describe' => 'nullable|string',
                'password' => 'required|string|min:8',
            ],[],[
                'username' => '用户名',
                'describe' => '描述',
                'password' => '密码',
            ]);

            //数据库操作
            $user_data = [];
            $frame_data = [];
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && trim($val)){
                    if($key == 'password' && $val){
                        $val = Hash::make($val);
                    }
                    $user_data[$key] = $val;
                }
            }
            $user = Users::create($user_data);

            if ($obj->level != 3){
                $frame_data['branch_id'] = $obj->branch_id;
                $frame_data['level'] = $obj->level+1;
                $frame_data['uid'] = $user->id;
                $frame_data['belong'] = $obj->id;
            }else{
                $frame_data['branch_id'] = $obj->branch_id;
                $frame_data['level'] = $obj->level;
                $frame_data['uid'] = $user->id;
                $frame_data['belong'] = $obj->belong;
            }


            $frame = Frame::create($frame_data);

            $user->frame_id = $frame->id;

            $user->save();

            $user_info = User::with('frame')->find($user->id);
            if ($user_info && $user_info->type == 1){
                if ($user_info->frame){
                    switch ($user_info->frame->level ){
                        case 1:
                            $user_info->assignRole('admin');
                            break;
                        case 2:
                            $user_info->assignRole('leader');
                            break;
                        case 3:
                            $user_info->assignRole('staff');
                            break;
                    }
                }
            }elseif ($user_info && $user_info->type == 2){
                $user_info->assignRole('external');
            }elseif ($user_info && $user_info->type == 3){
                $user_info->assignRole('recommend');
            }

            //返回结果
            return ['code'=>1];
        }

        return view('Home.User.Set.Frame.add',compact('obj'));
    }

    public function frame_edit(FrameModel $obj){
        $obj = $obj->load('user');
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'username' => ['nullable','string',function($attribute, $value, $fail) use($obj){
                    if($obj->user->username != $value){
                        $user = UsersModel::where('username',$value)->where('status',1)->first();
                        if ($user){
                            $fail(':attribute 已存在1。');
                        }
                    }
                }],
                'describe' => 'nullable|string',
                'password' => 'nullable|string|min:8',
                'leader_id' => 'nullable|integer',
            ],[],[
                'username' => '用户名',
                'describe' => '描述',
                'password' => '密码',
                'leader_id' => '直属领导ID'
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && trim($val)){

                    if($key == 'password' && trim($val)){
                        $val = Hash::make($val);
                    }

                    $obj->user->$key = $val;
                }

                if ($key == 'leader_id'){
                    $leader = Frame::find(intval($val));
                    $obj->branch_id = $leader->branch_id;
                    $obj->level = $leader->level+1;
                    $obj->belong = $leader->id;
                    $obj->save();
                }
            }
            $res = $obj->user->save();

            foreach ($this->roleList as $role){
                $obj->user->removeRole($role);
            }

            if ($obj->user->type == 1){
                switch ($obj->level ){
                    case 1:
                        $obj->user->assignRole('admin');
                        break;
                    case 2:
                        $obj->user->assignRole('leader');
                        break;
                    case 3:
                        $obj->user->assignRole('staff');
                        break;
                }
            }elseif ($obj->user->type == 2){
                $obj->user->assignRole('external');
            }elseif ($obj->user->type == 3){
                $obj->user->assignRole('recommend');
            }

            //返回结果
            return ['code'=>1];
        }
        $leader = Frame::getLeader($obj->branch_id,$obj->level,false);

        return view('Home.User.Set.Frame.edit',compact('obj','leader'));

    }

    public function frame_del(FrameModel $obj){

        $obj->load('user');

        $frame_list = Frame::GetDelFrame($obj->id);

        $frame_list[] = $obj;

        foreach($frame_list as $frame){

            if ($frame->user){

                $frame->user->status = -1;

                $frame->user->save();

            }

            $frame->status = -1;

            $frame->save();

        }

        return ['code'=>1];

    }

}